package com.area.cth.controller;

import com.area.cth.pojo.Goods;
import com.area.cth.common.Result;
import com.area.cth.service.GoodsService;
import com.area.cth.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "商品相关接口")
@RestController
@RequestMapping("/goods")
@CrossOrigin
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService gs;

    @Operation(summary = "新增商品")
    @PostMapping("/addGoods")
    @CacheEvict(cacheNames = "goodsCache", key = "#g.category")
    public Result addGoods(@RequestBody @Validated Goods g) {
        if (g.getPrice() <= 0) {
            throw new RuntimeException("价格必须大于0");
        }
        log.info("新增商品：{}", g);
        gs.addGoods(g);
        return new Result(0, "发布成功", null);
    }

    @Operation(summary = "查询全部商品")
    @GetMapping
    public Result<List<Goods>> goodsList() {
        List<Goods> goodsList = gs.getGoodsList();
        return Result.success(goodsList);
    }

    @Operation(summary = "分页查询")
    @GetMapping("/list")
    public Result<IPage<Goods>> getGoodsList(Integer pageNo, Integer pageSize,@RequestParam(required = false) Integer auditState, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String keyword) {
        IPage<Goods> goodsList = gs.goodsList(pageNo, pageSize, auditState,categoryId, keyword);
        return Result.success(goodsList);
    }

    @Operation(summary = "查询个人发布的商品")
    @GetMapping("/myGoods")
    public Result<List<Goods>> myGoodsList() {
        List<Goods> myGoodsList = gs.getMyGoodsList();
        return Result.success(myGoodsList);
    }

    @Operation(summary = "根据审核状态查询个人发布的商品")
    @GetMapping("/myGoodsList/{state}")
    public Result<List<Goods>> myGoodsList(@PathVariable Integer state) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer uid = (Integer) map.get("id");
        List<Goods> list = gs.lambdaQuery()
                .eq(Goods::getCreateUser, uid)
                .eq(Goods::getAuditState, state)
                .list();
        return Result.success(list);
    }

    @Operation(summary = "根据发布者id查询商品")
    @GetMapping("/getGoodsByUser")
    public Result<List<Goods>> getGoodsByUserList(Integer userId) {
        List<Goods> getGoodsByUserList = gs.getGoodsByUserList(userId);
        return Result.success(getGoodsByUserList);
    }

    @Operation(summary = "搜索")
    @GetMapping("/search")
    public Result<List<Goods>> searchList(String keyword) {
        List<Goods> searchList = gs.searchList(keyword);
        return Result.success(searchList);
    }

    @Operation(summary = "根据分类id查询商品")
    @GetMapping("/getByCategory")
    @Cacheable(cacheNames = "goodsCache", key = "#category")
    public Result<List<Goods>> goodsListByCategory(Integer category) {
        List<Goods> goodsListByCategory = gs.getGoodsListByCategory(category);
        return Result.success(goodsListByCategory);
    }

    @Operation(summary = "根据id查询商品")
    @GetMapping("/getById")
    public Result<Goods> goodsById(Integer id) {
        Goods goodsById = gs.getGoodsById(id);
        return Result.success(goodsById);
    }

    @Operation(summary = "修改商品")
    @PutMapping
    @CacheEvict(cacheNames = "goodsCache", allEntries = true)
    public Result updateGoods(@RequestBody @Validated Goods g) {
        gs.update(g);
        return new Result(0, "修改成功", null);
    }

    @Operation(summary = "删除商品")
    @DeleteMapping
    @CacheEvict(cacheNames = "goodsCache", allEntries = true)
    public Result deleteGoods(Integer id) {
        gs.deleteGoods(id);
        return new Result(0, "下架成功", null);
    }

    @Operation(summary = "修改商品审核状态")
    @PutMapping("/updateState")
    @CacheEvict(cacheNames = "goodsCache", allEntries = true)
    public Result updateState(Integer goodsId, Integer state) {
        gs.lambdaUpdate()
                .set(Goods::getAuditState, state)
                .eq(Goods::getId, goodsId)
                .update();
        return Result.success();
    }

    @Operation(summary = "增加浏览量")
    @PutMapping("/addVisit")
    public Result addVisit(Integer id) {
        Goods g = gs.getGoodsById(id);
        Long n = g.getVisit();
        if (n == null) {
            gs.lambdaUpdate()
                    .set(Goods::getVisit, 1)
                    .eq(Goods::getId, id)
                    .update();
        } else {
            gs.lambdaUpdate()
                    .set(Goods::getVisit, n + 1)
                    .eq(Goods::getId, id)
                    .update();
        }
        return Result.success();
    }

    @Operation(summary = "获取浏览量")
    @GetMapping("/getVisits")
    public Result getVisits(Integer id) {
        Goods g = gs.getGoodsById(id);
        return Result.success(g.getVisit());
    }
}
